/**
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: asiainfo </p>
 */
package com.webull.information.center.dao;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import java.util.ArrayList;
import java.util.List;

/**
 * {类描述}.
 *
 * @author longyang
 * @version 1.0
 * @date 2016年5月16日 上午10:38:37
 * @since JDK 1.8
 */
public class JdbcHelper {

    public static SqlParameterSource getSqlParameterSource(Object object) {
        if (object != null)
            return new BeanPropertySqlParameterSource(object);
        return null;
    }

    public static SqlParameterSource[] getSqlParameterSources(List objects) {
        if (objects != null) {
            List<SqlParameterSource> objectList = new ArrayList<SqlParameterSource>();
            for (Object ob : objects) {
                BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(ob);
                objectList.add(source);
            }
            return objectList.toArray(new SqlParameterSource[objectList.size()]);
        }
        return null;
    }

    public static <T> RowMapper<T> getRowMapper(Class<T> mappedClass) {
        RowMapper<T> rowMapper = BeanPropertyRowMapper.newInstance(mappedClass);
        return rowMapper;
    }

}
